﻿using System;
using Microsoft.AspNetCore.Mvc;

namespace FlashPay.Management.Controllers
{
    using FlashPay.Management.ViewModels.User;
    using FlashPay.Entity;
    using FlashPay.Entity.Response.User;
    using FlashPay.Service.Interface;
    using FlashPay.Util;

    /// <summary>
    /// 账号控制器
    /// </summary>
    public class AccountController : Controller
    {
        #region 注入
        /// <summary>
        /// 缓存
        /// </summary>
        private readonly MemoryCacheUtil _memoryCacheUtil;

        /// <summary>
        /// 用户业务接口
        /// </summary>
        private readonly UserInfoService _userInfoService;

        /// <summary>
        /// 授权业务接口
        /// </summary>
        private readonly AuthorizeService _authorizeService;

        /// <summary>
        /// 权限业务接口
        /// </summary>
        private readonly PermissionService _permissionService;

        /// <summary>
        /// 权限业务接口
        /// </summary>
        private readonly AuthService _authService;

        /// <summary>
        /// 票据
        /// </summary>
        private readonly Authenticate<TicketResponse> _authenticate;

        /// <summary>
        /// 注入
        /// </summary>
        /// <param name="sysRoleService"></param>
        /// <param name="userInfoService"></param>
        /// <param name="authorizeService"></param>
        public AccountController(UserInfoService userInfoService, AuthorizeService authorizeService, PermissionService permissionService, AuthService authService, Authenticate<TicketResponse> authenticate)
        {
            _userInfoService = userInfoService;
            _authorizeService = authorizeService;
            _permissionService = permissionService;
            _authService = authService;
            _authenticate = authenticate;
        }
        #endregion

        #region 登录
        public IActionResult Login()
        {
            return View();
        }

        /// <summary>
        /// 获取Token对象
        /// </summary>
        /// <param name="para">参数</param>
        /// <returns></returns>
        public JsonResult Authorize(AuthorizeViewModel para)
        {
            var response = new JResult<TokenResponse>()
            {
                Success = false
            };

            if (string.IsNullOrEmpty(para.Account))
            {
                response.ErrorMessage = "账号或密码错误！";
                return Json(response);
            }

            if (string.IsNullOrEmpty(para.Password))
            {
                response.ErrorMessage = "账号或密码错误！";
                return Json(response);
            }
            //登录参数

            var loginRequest = new Entity.Request.User.LoginRequest()
            {
                Account = para.Account,
                Password = para.Password,
                //日志
                Ip = HttpContext.GetUserIp(),
                CreateName = para.Account,
                RequestUrl = HttpContext.Request.Host.ToString(),
                //JsonConvert.SerializeObject(para)
                RequestData = "",
            };

            var result = _userInfoService.Login(loginRequest);
            if (result.Success)
            {
                _authenticate.data = new TicketResponse()
                {
                    UserId = result.Data.UserId,
                    UserName = result.Data.UserName,
                    CompanyId = result.Data.CompanyId,
                    MType = 1,
                    Host = Request.Host.ToString(),
                    PermissionsCode = result.Data.PermissionsCode
                };

                var ticketResult = _authenticate.SetTicket();
                if (ticketResult.Success)
                {

                    response.Success = true;
                    response.Data = new TokenResponse()
                    {
                        UserId = result.Data.UserId,
                        CompanyId = result.Data.CompanyId,
                        CompanyName = result.Data.CompanyName,
                        UserName = result.Data.UserName,
                        Token = ticketResult.Data
                    };
                    response.SuccessMessage = "登录成功！";
                }
            }
            else
            {
                response.Success = false;
                response.ErrorMessage = result.ErrorMessage;
            }
            return Json(response);
        }

        /// <summary>
        /// Token验证()
        /// </summary>
        /// <param name="token">token</param>
        /// <returns>JResult</returns>
        [HttpPost]
        [Obsolete("废弃")]
        public JsonResult VerifyToken(VerifyToken para)
        {
            var response = new JResult()
            {
                Success = false
            };

            if (string.IsNullOrEmpty(para.Token))
            {
                response.ErrorMessage = "参数错误！";
                return Json(response);
            }

            var deToken = EncryptHelper.deCryptDES(para.Token);
            if (string.IsNullOrEmpty(deToken))
            {
                response.ErrorMessage = "内部错误,请联系管理员！";
                return Json(response);
            }

            //对域名验证
            var host = Request.Host.ToString();

            var tokenResponse = _memoryCacheUtil.Get<TokenResponse>(deToken);
            if (tokenResponse == null)
            {
                response.ErrorMessage = "超时，请重新登录！";
                return Json(response);
            }

            if (tokenResponse.Token != para.Token)
            {
                response.ErrorMessage = "超时，请重新登录！";
                return Json(response);
            }

            response.Success = true;
            return Json(response);
        }


        #endregion
    }
}